package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.excel.subjectlistener;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.One;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-10-29
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {



    public void savesubject(MultipartFile file,EduSubjectService subjectService) {
        try{
            InputStream in=file.getInputStream();

            EasyExcel.read(in, SubjectData.class,new subjectlistener(subjectService)).sheet().doRead();
    }catch(Exception e){

        }

    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        QueryWrapper<EduSubject> wrapperOne=new QueryWrapper();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectslist = baseMapper.selectList(wrapperOne);

        QueryWrapper<EduSubject> wrapperTwo=new QueryWrapper();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectslist = baseMapper.selectList(wrapperTwo);

        List<OneSubject> finalSubjectList=new ArrayList<>();
        for(int i=0;i<oneSubjectslist.size();i++){
            EduSubject eduSubject=oneSubjectslist.get(i);
            OneSubject oneSubject=new OneSubject();
            BeanUtils.copyProperties(eduSubject,oneSubject);
            finalSubjectList.add(oneSubject);
            List<TwoSubject> twoFinalSubjectList=new ArrayList<>();

            for(int m=0;m<twoSubjectslist.size();m++){
                EduSubject tSubject = twoSubjectslist.get(m);
                if(tSubject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject=new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoFinalSubjectList);
        }

return finalSubjectList;
    }
}
